package com.example.smartagriculture.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartagriculture.utils.Result;
import com.example.smartagriculture.service.IOutputService;
import com.example.smartagriculture.entity.Output;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;

import org.springframework.web.bind.annotation.RestController;

/**
 * OutputController 前端控制器
 * @author 张芸博
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/output")
public class OutputController {

    @Resource
    private IOutputService outputService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @Operation(description = "新增或更新")
    public Result save(@RequestBody Output output) {
        return Result.success(outputService.saveOrUpdate(output));
    }

    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @Operation(description = "通过id删除")
    public Result delete(@PathVariable Integer id) {
        Output output =outputService.getById(id);
        return Result.success(outputService.saveOrUpdate(output));
    }

    /**
    *  查询所有
    */
    @GetMapping("/all")
    @Operation(description = "查询所有")
    public Result findAll() {
        QueryWrapper<Output> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<Output> list=  outputService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    @Operation(description = "根据id查询")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(outputService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    @Operation(description = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Output> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(outputService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

